package random.fox.p2p.ui.customview;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import random.fox.p2p.R;
import random.fox.p2p.bean.PersonalityLabel;

/**
 * Created by delong on 2015/3/19.
 */
public class PersonalityAdapter extends BaseAdapter {

    private int[] bgs = {R.drawable.personality_bg_zero, R.drawable.personality_bg_one, R.drawable.personality_bg_two,
            R.drawable.personality_bg_three ,R.drawable.personality_bg_four};
    private Context context;
    private List<PersonalityLabel> list;
//    private int itemHeight;

    private int count;

    public PersonalityAdapter(List<PersonalityLabel> personalityLabels, int count) {
        this.list = personalityLabels;
//        this.itemHeight = itemHeight;
        this.count = count;

    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(context ==null)
            this.context = parent.getContext();
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.personality_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        int index = position % 5;
        if (index == 0) {
            holder.personalityItemLayout.setGravity(Gravity.RIGHT);
        } else if (index == 1) {
            holder.personalityItemLayout.setGravity(Gravity.LEFT);
        } else if (index == 2) {
            holder.personalityItemLayout.setGravity(Gravity.CENTER);
        } else if (index == 3) {
            holder.personalityItemLayout.setGravity(Gravity.LEFT);
        } else {
            holder.personalityItemLayout.setGravity(Gravity.RIGHT);
        }
        holder.personalityBg.setBackgroundResource(bgs[index]);

//        convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight));

        PersonalityLabel personalityLabel = list.get(position);
        String label = personalityLabel.getLable();
        String count = personalityLabel.getCount() + "";
        holder.personalityText.setText(label);
        holder.personalityCountText.setText(count);
        return convertView;
    }

    class ViewHolder {
        @InjectView(R.id.personality_item_layout)
        LinearLayout personalityItemLayout;
        @InjectView(R.id.personality_bg_layout)
        LinearLayout personalityBg;

        @InjectView(R.id.personality_label_text)
        TextView personalityText;
        @InjectView(R.id.personality_label_count)
        TextView personalityCountText;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }

    }
}
